"""
已有方法rand7可生成 1 到 7 范围内的均匀随机整数，试写一个方法rand10生成 1 到 10 范围内的均匀随机整数。
不要使用系统的Math.random()方法。

示例 1:
输入: 1
输出: [7]

示例 2:
输入: 2
输出: [8,4]

示例 3:
输入: 3
输出: [8,1,10]

链接：https://leetcode-cn.com/problems/implement-rand10-using-rand7
"""
from mode import random


# The rand7() API is already defined for you.
# def rand7():
# @return a random integer in the range 1 to 7
def rand7():
    return random.randint(1, 7)


class Solution:
    def rand10(self):
        """
        :rtype: int
        """""
        while True:
            # rand7生成范围 [1,2,3,4,5,6,7]
            # rand7 - 1生成范围 [0,1,2,3,4,5,6]
            # a生成范围 [0,7,14,21,28,35,42]
            a = (rand7() - 1) * 7
            # rand7生成范围 [1,2,3,4,5,6,7]
            b = rand7()
            # c生成范围 [1 -> 49]
            c = a + b
            # d 范围 [0 -> 48]
            d = c - 1
            if d < 39:
                return d // 4 + 1


class Solution1:
    def rand10(self):
        """
        :rtype: int
        """
        while True:
            seed = (rand7() - 1) * 7 + rand7() - 1
            if 1 <= seed <= 10:
                return seed


if __name__ == "__main__":
    A = Solution1()
    res = []
    for _ in range(100):
        res.append(A.rand10())
        # print(A.rand10())
    print(res)
